#include <applicationTask.h>
#include <ColorMixingTask.h>
#include <lin_device.h>
#include <adc_device.h>
#include <uart_device.h>
#include <gpio_device.h>
#include <linMasterTask.h>


TaskState_t applState = TASK_STATE_INIT;
void ApplTimerExpired(SoftTimer_t *timer);

void colorCompensationTimerExpired(SoftTimer_t *timer);

#define FADING_TIME    200

SoftTimer_t ApplTimer = {
    .mode     = TIMER_PERIODIC_MODE,
    .interval = FADING_TIME,
    .handler  = ApplTimerExpired
};

SoftTimer_t colorCompensationTimer = {
    .mode     = TIMER_PERIODIC_MODE,
    .interval = 2000,
    .handler  = colorCompensationTimerExpired
};

uint16_t level;

typedef struct{
    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t level;
}RGB_t;

typedef struct{
    uint16_t red;
    uint16_t green;
    uint16_t blue;
}PWMT_t;

#define TABLE_SIZE (36+1)
const RGB_t rgbTable[LED_MAX_NUM*TABLE_SIZE] = {
  {255,0,0,100},{0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {255,0,0,100},{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{255,0,0,100}, {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {255,0,0,100}, {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},    {255,0,0,100},  {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},    {0,0,0,10},     {255,0,0,100},
  
  {0,255,0,100},{0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,255,0,100},{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,255,0,100}, {0,0,0,0},      {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,255,0,100}, {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,255,0,100}, {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,0,0,10},    {0,255,0,100},
  
  {0,0,255,100},{0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,255,100},{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,255,100}, {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,0,255,100}, {0,0,0,10},     {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,0,255,100}, {0,0,0,10},
  {0,0,0,10},    {0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,255,100},
  
  {255,0,0,100},{0,0,0,10}    ,{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {255,0,0,100},{255,0,0,100},{0,0,0,10},     {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {255,0,0,100},{255,0,0,100},{255,0,0,100}, {0,0,0,10},     {0,0,0,10},     {0,0,0,10},
  {255,0,0,100},{255,0,0,100},{255,0,0,100},{255,0,0,100},  {0,0,0,10},     {0,0,0,10},
  {255,0,0,100},{255,0,0,100},{255,0,0,100},{255,0,0,100},  {255,0,0,100}, {0,0,0,10},
  {255,0,0,100},{255,0,0,100},{255,0,0,100},{255,0,0,100},  {255,0,0,100}, {255,0,0,100},
  
  {0,255,0,100},{255,0,0,100},{255,0,0,100},{255,0,0,100},  {255,0,0,100}, {255,0,0,100},
  {0,255,0,100},{0,255,0,100},{255,0,0,100},{255,0,0,100},  {255,0,0,100}, {255,0,0,100},
  {0,255,0,100},{0,255,0,100},{0,255,0,100},{255,0,0,100},  {255,0,0,100}, {255,0,0,100},
  {0,255,0,100},{0,255,0,100},{0,255,0,100},{0,255,0,100},  {255,0,0,100}, {255,0,0,100},
  {0,255,0,100},{0,255,0,100},{0,255,0,100},{0,255,0,100},  {0,255,0,100}, {255,0,0,100},
  {0,255,0,100},{0,255,0,100},{0,255,0,100},{0,255,0,100},  {0,255,0,100}, {0,255,0,100},
  
  {0,0,255,100},{0,255,0,100},{0,255,0,100},{0,255,0,100},  {0,255,0,100}, {0,255,0,100},
  {0,0,255,100},{0,0,255,100},{0,255,0,100},{0,255,0,100},  {0,255,0,100}, {0,255,0,100},
  {0,0,255,100},{0,0,255,100},{0,0,255,100},{0,255,0,100},  {0,255,0,100}, {0,255,0,100},
  {0,0,255,100},{0,0,255,100},{0,0,255,100},{0,0,255,100},  {0,255,0,100}, {0,255,0,100},
  {0,0,255,100},{0,0,255,100},{0,0,255,100},{0,0,255,100},  {0,0,255,100}, {0,255,0,100},
  {0,0,255,100},{0,0,255,100},{0,0,255,100},{0,0,255,100},  {0,0,255,100}, {0,0,255,100},
  

};


uint8_t tableIndex = 0;

uint8_t buff[3] = {0x055,0x0AA, 0x00};

void ApplTimerExpired(SoftTimer_t *timer)
{
    /*CLM_SetRGBL(ROOM_TEMPERATURE, LED0,0, 0, 255, 100, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED1,0, 0, 255, 100, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED2,0, 0, 255, 100, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED3,0, 0, 255, 100, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED4,0, 0, 255, 100, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED5,0, 0, 255, 100, FADING_TIME);*/
    CLM_SetRGBL(ROOM_TEMPERATURE, LED0,rgbTable[tableIndex*LED_MAX_NUM +0].red, rgbTable[tableIndex*LED_MAX_NUM +0].green, rgbTable[tableIndex*LED_MAX_NUM +0].blue, rgbTable[tableIndex*LED_MAX_NUM +0].level, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED1,rgbTable[tableIndex*LED_MAX_NUM +1].red, rgbTable[tableIndex*LED_MAX_NUM +1].green, rgbTable[tableIndex*LED_MAX_NUM +1].blue, rgbTable[tableIndex*LED_MAX_NUM +1].level, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED2,rgbTable[tableIndex*LED_MAX_NUM +2].red, rgbTable[tableIndex*LED_MAX_NUM +2].green, rgbTable[tableIndex*LED_MAX_NUM +2].blue, rgbTable[tableIndex*LED_MAX_NUM +2].level, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED3,rgbTable[tableIndex*LED_MAX_NUM +3].red, rgbTable[tableIndex*LED_MAX_NUM +3].green, rgbTable[tableIndex*LED_MAX_NUM +3].blue, rgbTable[tableIndex*LED_MAX_NUM +3].level, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED4,rgbTable[tableIndex*LED_MAX_NUM +4].red, rgbTable[tableIndex*LED_MAX_NUM +4].green, rgbTable[tableIndex*LED_MAX_NUM +4].blue, rgbTable[tableIndex*LED_MAX_NUM +4].level, FADING_TIME);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED5,rgbTable[tableIndex*LED_MAX_NUM +5].red, rgbTable[tableIndex*LED_MAX_NUM +5].green, rgbTable[tableIndex*LED_MAX_NUM +5].blue, rgbTable[tableIndex*LED_MAX_NUM +5].level, FADING_TIME);

    tableIndex++;
    if (tableIndex>=TABLE_SIZE){
        tableIndex = 0;
    }
    buff[2] = tableIndex;
//    UART_SendData(buff, 3);
}

void Uart_RX_ISR(uint8_t data)
{
  
}


void APPL_TaskHandler(void)
{
    switch(applState){
    case TASK_STATE_INIT:
      SoftTimer_Start(&ApplTimer);
//      SoftTimer_Start(&colorCompensationTimer);
      applState = TASK_STATE_ACTIVE;
      
//      UART_Init(BAUDRATE_9600, BITSIZE_8BITS, PARITY_NONE,STOPS_1BITS);
//      UART_RegisterIRQ(Uart_RX_ISR, NULL);
      
      GPIO_Init(GPIO_PORT_1, GPIO_MUX_GPIO, GPIO_DIR_OUTPUT,GPIO_PULL_MODE_NONE);
      GPIO_Init(GPIO_PORT_2, GPIO_MUX_GPIO, GPIO_DIR_OUTPUT,GPIO_PULL_MODE_NONE);
      GPIO_Init(GPIO_PORT_3, GPIO_MUX_GPIO, GPIO_DIR_OUTPUT,GPIO_PULL_MODE_NONE);
      
      
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), 0);
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_1));
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_2));
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_2) | (1 << GPIO_PORT_1));
      
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3));
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3) | (1 << GPIO_PORT_1));
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3) | (1 << GPIO_PORT_2));
      GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_1));

      break;
    case TASK_STATE_ACTIVE:
      break;
    }
}


void colorCompensationTimerExpired(SoftTimer_t *timer)
{
//    uint8_t ledNo = 0;
//    uint16_t pwm[3];
//    CLM_GetCurrentPWMs(&pwm[LED_RED], &pwm[LED_GREEN], &pwm[LED_BLUE]);
////    DEBUG_OUT("PWM R: %d, PWM_G: %d, PWM_B: %d \r\n",pwm[CHANNEL_RED],pwm[CHANNEL_GREEN],pwm[CHANNEL_BLUE]);
//    if (CLM_FadingIsFinished() == true){
//        ColorParam_t *color = CLM_GetTargetColor();
//        int16_t temperature = ADC_GetLedTemperature();
////        DEBUG_OUT("T: %d\r\n",temperature);
//        switch(color->colorMode){
//        case COLOR_MODE_RGB:
//          CLM_SetRGB(temperature, ledNo, color->RGB.red, color->RGB.green, color->RGB.blue,1000);
//          break;
//        case COLOR_MODE_RGBL:
//          CLM_SetRGBL(temperature, ledNo, color->RGBL.red, color->RGBL.green, color->RGBL.blue,color->RGBL.level,1000);
//          break;
//        case COLOR_MODE_HSL:
//          CLM_SetHSL(temperature, ledNo, color->HSL.hue, color->HSL.saturation, color->HSL.level, 1000);
//          break;
//        case COLOR_MODE_XYY:
//          CLM_SetXYY(temperature, ledNo, color->XYY.x, color->XYY.y, color->XYY.Y, 1000);
//          break;
//        }
//    }
}



